﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;
using System.Diagnostics;

namespace InsideDL
{
    public partial class frmIMImport : MISControl.frmMatrix
    {
        protected EXLAPP exlAPP = null;
        protected string bat = "TransIM001.bat";

        public frmIMImport()
        {
            InitializeComponent();

            ucIMFile.SelectClick += new EventHandler(getExcelSheetList);
            this.controlList = new List<ucSelFileOrPath>
            {
                ucIMFile
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);

            this.FormClosed += new FormClosedEventHandler(frmIMImport_FormClosed);
                
        }

        /// <summary>
        /// 生出可以執行的BAT檔
        /// </summary>
//        void createSQLBat()
//        { 
//            string str,tmp;
//            str = string.Format(@"
//OSQL -S (local)\SQLEXPRESS -E -i {0}\IM001.sql
//PAUSE
//",Program.TmpPath());
//            tmp = string.Format("{0}\\{1}",Program.TmpPath(),bat);
//            Program.CreateFile(tmp, str);            
//        }

        void frmIMImport_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (exlAPP != null)
            {
                exlAPP.SaveCloseXLS();
                exlAPP = null;
            }
        }

        protected void getExcelSheetList(object sender, EventArgs e)
        {
            exlAPP = new EXLAPP();
            exlAPP.OpenFile(ucIMFile.Txt);
            List<string> shtList = exlAPP.GetSheetList();
            //CheckBox chk = null;
            foreach (string str in shtList)
            {
                //chk = new CheckBox();
                //chk.Text = str;                
                //chkLB.Items.Add(chk);
                chkLB.Items.Add(str);
            }
            exlAPP.CloseFile();

        }

        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }

            DataIMImport im = new DataIMImport();
            
            //string ddd = im.buildInsertStatement("10003"
            //    , @"D:\MY\WORK\IM\科目_金控績效管理處_1003.xls");
            //List<string> rowList = new List<string>();

            StringBuilder sb = new StringBuilder();
                
            //找出有勾選的項目
            for(int i=0 ;i<chkLB.Items.Count;i++)
            {
                if (chkLB.GetItemChecked(i).Equals(true))
                {
                    //im.Check(chkLB.Items[i].ToString());
                    sb.Append(
                    im.buildInsertStatement(chkLB.Items[i].ToString()
                        , ucIMFile.Txt));                        
                }
            }

            //把產生出來的資料存成 .sql
            string tmp,sqlFile;
            sqlFile = "IM001";
            tmp = string.Format("{0}\\{1}.sql", Program.TmpPath(),sqlFile);
            Program.CreateFile(tmp, sb.ToString());
            //createSQLBat();
            this.createSQLBat(bat, sqlFile);

            
            tmp = string.Format("{0}\\{1}", Program.TmpPath(), bat);
            this.executeBat(tmp);
            /*
            System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(tmp);
            p.WindowStyle =  ProcessWindowStyle.Normal;
            Process process = Process.Start(p);
            process.WaitForExit();
            if (!process.HasExited)
            {
                process.Kill();
            }
            showMessage(this.Text, "完成！");
             */ 
        }
        

       

        
    }

    
}
